/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controleur;

import application.ConstantPool;
import application.Facade;
import application.beans.CompteBean;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Étienne
 */
public class ServGestionCompte extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        Facade f = new Facade();
        ConstantPool instance = ConstantPool.getInstance();
        
        String next = ConstantesWeb.ATTRIBUT_PAGE_GESTIONCOMPTE;
        CompteBean compte = (CompteBean)session.getAttribute(ConstantesWeb.ATTRIBUT_UTILISATEUR);
        List<CompteBean> resultats = new ArrayList<CompteBean>();
        session.setAttribute(ConstantesWeb.ATTRIBUT_LISTE_SECTIONS, f.obtenirSections());
        
        // Si le compte est vide ou qu'il n'est pas admin, on va à la page d'accueil
        if(compte == null || !(compte.getStatut().equals("administrateur"))){
            next = ConstantesWeb.ATTRIBUT_SERV_ACCUEIL;
        }
        
        // On ajoute les attributs à la session
        session.setAttribute(ConstantesWeb.PARAM_NUM_PAGE, 1);
        session.setAttribute(ConstantesWeb.ATTRIBUT_RESULTAT_GESTION_COMPTE, resultats);
        session.setAttribute(ConstantesWeb.ATTRIBUT_DOMAINES, instance.getDomaines());
        session.setAttribute(ConstantesWeb.ATTRIBUT_STATUTS, instance.getStatuts());
        session.setAttribute(ConstantesWeb.ATTRIBUT_CRITERES_GESTION_COMPTE, new CompteBean());
        
        RequestDispatcher rd = getServletContext().getRequestDispatcher(next);
        rd.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
